package com.abc.cloud.dao;

import com.abc.cloud.dto.role.RoleDto;
import com.abc.cloud.entity.MenuEntity;
import com.abc.cloud.entity.PermsEntity;
import com.abc.cloud.entity.RoleEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 角色表
 * 
 * @author LiJunXi
 * @email 2770063826@qq.com
 * @date 2023-10-27 13:09:54
 */
@Mapper
public interface RoleDao extends BaseMapper<RoleEntity> {

    @Select("select r.name " +
            "from tb_user_role_relation ur, tb_role r " +
            "where ur.user_id = #{userId}")
    List<String> queryUserRoleList(Long userId);

    @Select("select r.name, r.id, r.role_value from tb_role r")
    List<RoleDto> selectRoleAll();

    int saveUserRole(@Param("list") List<Long> userRoleList,
                     @Param("userId") Long userId);

    @Select("select r.id " +
            "from tb_role r, tb_user_role_relation ar, tb_user a " +
            "where ar.user_id = a.id and ar.role_id = r.id and a.username = #{username_}")
    List<Long> selectUserRoleIdByUsername(String username);

    @Delete("delete from tb_user_role_relation where user_id = #{userId}")
    int deleteUserRole(Long userId);

    @Select("select count(*) from tb_role")
    Integer queryRoleTotal();

    List<RoleEntity> queryRoleInfoPage(@Param("i") int i,
                                       @Param("pageSize") Integer pageSize,
                                       @Param("content") String content);

    @Select("select am.id, am.name, am.title, am.hidden, am.sort, am.icon, am.parent_id, am.create_time, am.update_time," +
            "am.status " +
            "from tb_menu am, tb_role_menu_relation ram " +
            "where ram.role_id = #{roleId} and ram.menu_id = am.id")
    List<MenuEntity> queryRoleMenuList(Long roleId);

    @Delete("delete from tb_role_menu_relation where role_id = #{roleId}")
    int deleteRoleMenuByRoleId(Long roleId);

    int saveRoleMenu(@Param("roleId") Long roleId,
                     @Param("list") List<Long> adminMenuIds);

    @Select("select m.id, m.status, m.create_time, m.update_time, m.perms, m.name, m.perms_category_id " +
            "from tb_role_perms_relation rc, tb_perms m " +
            "where rc.role_id = #{roleId} and rc.perms_id = m.id")
    List<PermsEntity> selectRolePermsList(Long roleId);

    @Delete("delete from tb_role_perms_relation where role_id = #{roleId}")
    int deleteRolePermsByRoleId(Long roleId);

    int saveRolePerms(@Param("roleId") Long roleId,
                      @Param("list") List<Long> adminMenuIds);

    @Select("select r.id " +
            "from tb_role r, tb_user_role_relation ar, tb_user a " +
            "where ar.user_id = a.id and ar.role_id = r.id and a.username = #{username_}")
    List<String> selectUserRoleByUsername(String username);

    void updateStatusByIds(@Param("list") List<Long> asList, @Param("status") Integer status);
}
